<template>
  <div >
    <div class="login-container">
      <el-card class="login-card">
        <h2 class="login-title">供应商-账户登录</h2>
        <el-form ref="loginForm" :model="loginForm" :rules="rules" label-position="left">
          <el-form-item label="用户名" prop="username">
            <el-input v-model="loginForm.username" placeholder="请输入用户名"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input type="password" v-model="loginForm.password" placeholder="请输入密码"></el-input>
          </el-form-item>
          <el-form-item align="center">
            <el-button type="primary"  @click="handleLogin">登录</el-button>
            <el-button type="success"  @click="show">注册</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
    <div align="center">
      <a href="#">关于我们</a> &nbsp;&nbsp;&nbsp;&nbsp;
      <a href="#">联系我们</a> &nbsp;&nbsp;&nbsp;&nbsp;
      <a href="#">联系客服</a> &nbsp;&nbsp;&nbsp;&nbsp;
      <a href="#">商家入驻</a> &nbsp;&nbsp;&nbsp;&nbsp;
      <a href="#">营销中心</a> &nbsp;&nbsp;&nbsp;&nbsp;
      <a href="#">手机东东商城</a> &nbsp;&nbsp;&nbsp;&nbsp;
      <a href="#">销售联盟</a> &nbsp;&nbsp;&nbsp;&nbsp;
      <a href="#">东东商城社区</a> &nbsp;&nbsp;&nbsp;&nbsp;  <br/><br/>
      <span>地址：北京市昌平区沙阳路18号 邮编：100000 电话：400-0123-302</span> <br/>
      <span>京ICP备08006666号京公网安备11010888888</span>
    </div>
<!--    注册模态框-->
    <el-dialog title="注册账户" :visible.sync="dialogFormVisible" width="500px">
      <el-form :model="registerForm">
        <el-form-item label="账号" :label-width="formLabelWidth">
          <el-input v-model="registerForm.sellerId" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" :label-width="formLabelWidth">
          <el-input v-model="registerForm.password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="昵称" :label-width="formLabelWidth">
          <el-input v-model="registerForm.nickName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="联系方式" :label-width="formLabelWidth">
          <el-input v-model="registerForm.linkmanMobile" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="register">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import axios from "axios";

export default {
  data() {
    return {
      dialogFormVisible: false,
      formLabelWidth: '80px',
      registerForm: {
        sellerId: '',
        password: '',
        nickName: '',
        linkmanMobile: ''
      },
      loginForm: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      }
    };
  },
  methods: {
    //账户注册
    show() {
      this.dialogFormVisible = true;
    },
    register() {
      console.log(this.registerForm);
      axios.post("/seller/register",this.registerForm).then(resp=>{
        if (resp.data.code === "1") {
          this.$message({
            message: '注册成功',
            type: 'success'
          });
          this.dialogFormVisible = false;
        } else {
          this.$message({
            message: '注册失败',
            type: 'error'
          });
        }
      })
    },
    //账户登陆
    handleLogin() {
      this.$refs.loginForm.validate(valid => {if (valid) {          // 这里可以进行登录逻辑，例如发送请求到后端验证用户信息          this.$message.success('登录成功');

         // 发送登录请求
         this.$http.post("/login/sellerLogin",{username:this.loginForm.username,password: this.loginForm.password})
             .then(resp=>{
              var result = resp.data;

              if(result.code==="2"){
                var token = result.data.token;
                var username = result.data.username;
                console.log(result);
                //存入localstorage
                localStorage.setItem("token",token);
                localStorage.setItem("username",username);
                this.$message({
                  message: '登陆成功',
                  type: 'success'
                });
                this.$router.push("/");
              }else{
                alert(result.message);
              }

             }).catch(function (resp){
                alert("网络异常，稍后重试");
         })

      } else {
          return false;
        }
      });
    }
  }
};
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 60vh;
  background-color: #f5f5f5;
  background-image: url('@/assets/img/manage-bg.png'); /* 替换为你的图片路径 */
}

.login-card {
  width: 400px;
}

.login-title {
  text-align: center;
  margin-bottom: 20px;
}

.login-btn {
  width: 100%;
}

</style>